package com.river.boot.biz.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.river.boot.biz.converter.ProductCategoryConverter;
import com.river.boot.biz.mapper.ProductCategoryMapper;
import com.river.boot.biz.model.entity.ProductCategory;
import com.river.boot.biz.model.form.ProductCategoryForm;
import com.river.boot.biz.model.query.ProductCategoryQuery;
import com.river.boot.biz.model.vo.ProductCategoryVO;
import com.river.boot.biz.service.ProductCategoryService;
import com.river.boot.common.base.Option;
import com.river.boot.security.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜品分类服务实现类
 *
 * @author JiangCheng Xiang
 * @since 2025-04-24 17:12
 */
@Service
@RequiredArgsConstructor
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements ProductCategoryService {

    private final ProductCategoryConverter productCategoryConverter;

    /**
     * 获取菜品分类分页列表
     *
     * @param queryParams 查询参数
     * @return {@link IPage<ProductCategoryVO>} 菜品分类分页列表
     */
    @Override
    public IPage<ProductCategoryVO> getProductCategoryPage(ProductCategoryQuery queryParams) {
        Page<ProductCategoryVO> pageVO = this.baseMapper.getProductCategoryPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }

    @Override
    public List<Option<Long>> listCategoryOptions() {
        List<ProductCategory> categoryList = this.list(new LambdaQueryWrapper<ProductCategory>()
                .orderByAsc(ProductCategory::getSort)
        );
        return buildCategoryOptions(categoryList);
    }

    private List<Option<Long>> buildCategoryOptions(List<ProductCategory> categoryList) {
        return categoryList.stream()
                .map(category -> new Option<>(category.getId(), category.getName())).collect(Collectors.toList());
    }

    /**
     * 获取菜品分类表单数据
     *
     * @param id 菜品分类ID
     * @return
     */
    @Override
    public ProductCategoryForm getProductCategoryFormData(Long id) {
        ProductCategory entity = this.getById(id);
        return productCategoryConverter.toForm(entity);
    }

    /**
     * 新增菜品分类
     *
     * @param formData 菜品分类表单对象
     * @return
     */
    @Override
    public boolean saveProductCategory(ProductCategoryForm formData) {
        ProductCategory entity = productCategoryConverter.toEntity(formData);
        entity.setCreateBy(SecurityUtils.getUserId());
        entity.setUpdateBy(SecurityUtils.getUserId());
        return this.save(entity);
    }

    /**
     * 更新菜品分类
     *
     * @param id       菜品分类ID
     * @param formData 菜品分类表单对象
     * @return
     */
    @Override
    public boolean updateProductCategory(Long id, ProductCategoryForm formData) {
        ProductCategory entity = productCategoryConverter.toEntity(formData);
        entity.setUpdateBy(SecurityUtils.getUserId());
        return this.updateById(entity);
    }

    /**
     * 删除菜品分类
     *
     * @param ids 菜品分类ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteProductCategorys(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的菜品分类数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
